package com.shop.cloud.service.system.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.shop.cloud.common.mybatis.BaseService;
import com.shop.cloud.common.mybatis.MybatisDao;
import com.shop.cloud.dao.system.ExtSysResourceDao;
import com.shop.cloud.dao.system.ExtSysRoleDao;
import com.shop.cloud.dao.system.SysResourceDao;
import com.shop.cloud.domain.system.RoleRessources;
import com.shop.cloud.domain.system.SysResource;
import com.shop.cloud.service.system.ISysResourceService;

@Service("sysResourceService")
public class SysResourceService extends BaseService<SysResource> implements ISysResourceService{
    
    @Autowired
	private SysResourceDao sysResourceDao;
    @Autowired
   	private ExtSysResourceDao extSysResourceDao;
    @Autowired
    private ExtSysRoleDao extSysRoleDao;
    
	@Override
	public MybatisDao<SysResource> getEntityDao() {
		return sysResourceDao;
	}
	
	/* 
	* <p>Title: findRoleResource</p>
	* <p>Description: </p>
	* @param roleId
	* @return
	* @see com.magic.cloud.service.ddd#findRoleResource(java.lang.String)
	*/ 
	@Override
	public List<String> findRoleResource(Long roleId) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("roleId", roleId);
		return extSysResourceDao.findRoleResource(params);
	}
	
	/* 
	* <p>Title: findMenusByUserId</p>
	* <p>Description: </p>
	* @param userId
	* @param moudleId
	* @return
	* @see com.magic.cloud.service.ddd#findMenusByUserId(java.lang.String, java.lang.String)
	*/ 
	@Override
	public List<SysResource> findMenusByUserId(Long userId) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("userId", userId);
		return extSysResourceDao.findMenusByUserId(params);
	}

	/* 
	* <p>Title: findPermissionsByUserId</p>
	* <p>Description: </p>
	* @param userId
	* @return
	* @see com.magic.cloud.service.ddd#findPermissionsByUserId(java.lang.String)
	*/ 
	@Override
	public List<SysResource> findPermissionsByUserId(Long userId) {
		Map<String, Object> params=new HashMap<String, Object>();
		params.put("userId", userId);
		return extSysResourceDao.findPermissionsByUserId(params);
	}
	
	
	/* 
	* <p>Title: insert</p>
	* <p>Description: </p>
	* @param entity
	* @param roleIds
	* @return
	* @see com.magic.cloud.service.ddd#insert(com.magic.cloud.model.system.SysResource, java.lang.String)
	*/ 
	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public int insert(SysResource entity,String roleIds) {
		sysResourceDao.insert(entity);
		Long resourceId=entity.getId();
		if(StringUtils.isNotEmpty(roleIds)){
			String[] roleIds_=roleIds.split("[,]");
			List<RoleRessources> sRessources=new ArrayList<RoleRessources>();
			for (String roleId : roleIds_) {
				RoleRessources sRessources2=new RoleRessources();
				sRessources2.setResourceId(resourceId);
				sRessources2.setRoleId(Long.parseLong(roleId));
				sRessources.add(sRessources2);
			}
			extSysResourceDao.insertRoleResourceBatch(sRessources);
		}
		return 1;
	}

	@Override
	@Transactional(propagation=Propagation.REQUIRED)
	public void deleteResource(SysResource sysResource) {
		 int c=sysResourceDao.update(sysResource);
		 if(c>0){
			 Map<String, Object> params=new HashMap<String, Object>();
			 params.put("resourceId", sysResource.getId());
			 extSysRoleDao.removeByResourceId(params);
		 }
	}

	@Override
	public List<Map<String, Object>> list(Map<String, Object> params) {
		return extSysResourceDao.list(params);
	}

	/**
	 * 
	 * @param 根据角色 得到菜单
	 * @param gmc
	 * @param userId 用户id
	 * @return
	 */
	@Override
	public List<SysResource> findByUserId(SysResource sysResource) {

		return sysResourceDao.findByPositionId(sysResource);
	}

	/**
	 * @param 根据用户查询左侧菜单信息
	 * @param gmc
	 * @param 菜单资源
	 * @return
	 */
	@Override
	public List<SysResource> findByUser(SysResource sysResource) {
		
		return sysResourceDao.findByUserId(sysResource);
	}
	
    
}